var $_autoCompleteHelperConstants = function () 
{
	this.DATA_MODE_LOCAL = 'local';
	this.DATA_MODE_REMOTE = 'remote';
	this.CONCATENATE_ID = 'Id';
	this.MIN_LENGTH = 2;
};

	
var $_autoCompleteHelper = function (b) 
{	
	this.c = new $_autoCompleteHelperConstants();
	
	//Persistence data
	this.ui = undefined;
	this.id = valueConst.NULL_VALUE;
	this.text = '';
	
	//Settings
	this.textField = b.control;
	this.minLength = b.minLength || this.c.MIN_LENGTH;
	this.msgExists = b.msgExists || '';
	this.editMode = b.editMode || iConst.NEW;
	this.dataMode = b.dataMode || this.c.DATA_MODE_LOCAL;
	this.action = b.action;
	this.validate = b.validate || false;
	this.callBack = b.callBack || undefined;
	this.defaultId = b.defaultId || -1;
	this.defaultText = b.defaultText || '';
	
	if (this.dataMode == this.c.DATA_MODE_LOCAL) 
	{
		this.initAutoCompleteLocal();
	}
	else if(this.dataMode == this.c.DATA_MODE_REMOTE) 
	{
		this.initAutoCompleteRemote();
	}
	
	if (this.editMode == iConst.EDIT || this.editMode == iConst.CONSULT)
		this.populateData(b);
	else {
		if(this.defaultId != -1) {
			$("#"+this.textField).val(this.defaultText);
			this.id = this.defaultId;
			this.text = this.defaultText;
		}
	}
		
};

$_autoCompleteHelper.prototype.populateData = function (b)
{
	this.id = b.id || valueConst.NULL_VALUE;
	this.text = b.text || '';
	
	//Populate the Text Field
	$("#"+this.textField).val(this.text); 
	
};

$_autoCompleteHelper.prototype.initAutoCompleteLocal = function () 
{
	var h = this;

	$.ajax({
    	type: 'POST',
        url: h.action,
        data: '',
        dataType: 'json',
        success: function(jsonObj) {
        	$("#"+h.textField).autocomplete({
    			source:jsonObj,
    			minLength: h.minLength,
    			select: function(event, ui) { 
					h.itemSelected(ui);	
				},
    			change: function(event, ui) { 
					h.controlChanged(ui.item != null);
				}
        	});
        }
	});
};

$_autoCompleteHelper.prototype.initAutoCompleteRemote = function () 
{
	var h = this;
	
	$("#"+h.textField).autocomplete({
		source: h.action,
		minLength: h.minLength,
		select: function(event, ui) { 
			h.itemSelected(ui);
		},
		change: function(event, ui) { 
			h.controlChanged(ui.item != null);
		}
	});    
};

$_autoCompleteHelper.prototype.itemSelected = function (ui) 
{	
	this.ui = ui.item;
	this.id = ui.item.id;
	this.text = ui.item.value;
	
	if (this.callBack) 
		this.callBack();
	
};

$_autoCompleteHelper.prototype.controlChanged = function (clicked) 
{
	if (clicked)
		return;
	
	$("#" + this.textField).val('');
	
	if (this.validate) //force validation rule
		$("#" + this.textField).valid();
	
	this.ui = undefined;
	this.id = valueConst.NULL_VALUE;
	this.text = '';
	
	if (this.callBack) 
		this.callBack();
};

$_autoCompleteHelper.prototype.changeSource = function (source)
{
	if(this.dataMode == this.c.DATA_MODE_REMOTE) //Remote
	{
		if(source === undefined)
			$("#" + this.textField).autocomplete("option", "disabled", true);
		else {
			$("#" + this.textField).autocomplete("option", "source", source);
			$("#" + this.textField).autocomplete("option", "disabled", false);
		}
	}
	else if(this.dataMode == this.c.DATA_MODE_LOCAL){ //Local
		if(source === undefined)
			$("#" + this.textField).autocomplete("option", "disabled", true);
		else {
			this.action = source;
			this.initAutoCompleteLocal();	
		}
	}
		
};

$_autoCompleteHelper.prototype.serialize = function (firstConcat)
{	
	var retSerial = '';
	
	if (this.linkJqGrid == '') //Serialize text field Id
	{
		if (this.id == valueConst.NULL_VALUE) return retSerial;
		if (firstConcat) retSerial = '&';
		
		retSerial += this.textField + this.c.CONCATENATE_ID + '=' + this.id;	
	}
	else //Serialize grid Id's
	{
		if (this.jsonGrid.length == 0) return retSerial;
		if (firstConcat) retSerial = '&';
		
		retSerial += this.textField + this.c.CONCATENATE_ID + '=';
		for (var i=0;i < this.jsonGrid.length;i++)
		{
			retSerial += this.jsonGrid[i].id;
			retSerial += (i < this.jsonGrid.length-1) ? ',' : '';
		}
	}
	
	return retSerial;
};

$_autoCompleteHelper.prototype.reset = function ()
{
	this.id = -1;
	this.text = '';
	
	$("#" + this.textField).val('');	
}